package com.mikepenz.presenter.imp;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.mikepenz.app.R;
import com.mikepenz.interactor.CurrentCarInteractor;
import com.mikepenz.interactor.imp.CurrentCarInteractorImp;
import com.mikepenz.presenter.CurrentCarPresenter;
import com.mikepenz.presenter.listener.RequestListener;
import com.mikepenz.utils.CookicUntil;
import com.mikepenz.utils.MethodType;
import com.mikepenz.utils.SystemUtils;
import com.mikepenz.view.CurrentCarView;
import com.quickdv.until.LogHelper;

import java.util.Date;
import java.util.Timer;

/**
 * Created by lilongfei on 15/10/7.
 */
public class CurrentCarPresenterImp implements CurrentCarPresenter, RequestListener<JSONObject> {
    Context c;
    CurrentCarView view;
    CurrentCarInteractor interactor;
    LogHelper log;
    Timer time;
    Task task;
    public static Integer STOP = 3;

    public CurrentCarPresenterImp(Context c) {
        this(c, ((CurrentCarView) c));
    }

    public CurrentCarPresenterImp(Context c, CurrentCarView view) {
        this.c = c;
        this.view = view;
        interactor = new CurrentCarInteractorImp(c, this);
        log = LogHelper.getInstance();
    }

    @Override
    public void initViewAndEvent() {
        view.initViewAndEvent();
    }

    @Override
    public void refreshCurrentCar() {

//        view.showLoad();
        if (c != null) {
            String userId = CookicUntil.getUser().getUserId();
            String cardId = CookicUntil.getCardBean().getCardCode();
            if (!SystemUtils.checkUser(c)) {
                view.showError(c.getString(R.string.user_not_login));
                return;
            }
            interactor.refreshCurrentCar(userId, cardId);
        } else {
            if (task != null)
                task.cancel();
            if (time != null) {
                time.cancel();
            }
        }
    }

    @Override
    public void vehicles() {
        view.showLoad();
        String userId = CookicUntil.getUser().getUserId();
        String cardId = CookicUntil.getCardBean().getCardCode();
        if (!SystemUtils.checkUser(c)) {
            view.showError(c.getString(R.string.user_not_login));
            return;
        }
        interactor.vehicles(userId, cardId);
    }

    Date dt = new Date();
    Long temp_time = dt.getTime() - 40000;

    @Override
    public void Success(String tag, JSONObject result) {
        view.dismissLoad();
        if (tag.equals(MethodType.APPLICATION_RETURN)) {
            view.returnCar();
            view.dismissRutrunButton(false);
        } else if (tag.equals(MethodType.CURRENT_CAR)) {
            //TODO 逻辑不完善，需要在这里计算完所有内容后放给前台填充，暂时先已这种形式填充，后期改善
            if (result.get("messageInfo")!=null){
                stop();
                return;
            }
            Date dt = new Date();
            Long time = dt.getTime();
            if (c != null) {
                if (time - temp_time > 10000) {
                    view.refreshInfo(result);
                }
                temp_time = time;
            }
        }

    }

    @Override
    public void Error(String tag, String error) {
        view.dismissLoad();
        view.showError(error);
        if (tag.equals(MethodType.APPLICATION_RETURN)) {
            view.dismissRutrunButton(true);
        } else if (tag.equals(MethodType.CURRENT_CAR)) {
            view.setStatusVisibale(View.GONE);
            stop();
        }
    }

    @Override
    public void Exception(String tag, VolleyError error) {
        view.dismissLoad();
        view.showError(c.getString(R.string.system_error));

    }


    public void start() {
        time = new Timer();
        task = new Task();
        time.schedule(task, 0, 40000);//过1秒执行，之后每隔40秒执行一次

    }

    class Task extends java.util.TimerTask {

        public int i = 0;

        public void setI(int i) {
            this.i = i;
        }

        public void run() {
            if (i == STOP) {
                time.cancel();
                new Exception();
            }
            handler.sendEmptyMessage(0);
        }
    }

    public void stop() {
        task.setI(STOP);
        task.cancel();
        time.cancel();
        c = null;
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            refreshCurrentCar();
        }
    };


}
